﻿using System;
using System.Data.Common;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;

namespace ERP
{
    public partial class test : System.Web.UI.Page
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
        private static string database = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            //file upload path
            string path = fileuploadExcel.PostedFile.FileName;
            //Create Connection to Excel work book
            var excelConnection = new OleDbConnection(connectionString);
            //Create OleDbCommand to fetch data from Excel
            var cmd = new OleDbCommand("Select [Alias],[Name] from [Sheet1$]", excelConnection);
            excelConnection.Open();
            OleDbDataReader dReader;
            dReader = cmd.ExecuteReader();
            var sqlBulk = new SqlBulkCopy(database);
            //Give your Destination table name
            sqlBulk.DestinationTableName = "ImportExcel";
            sqlBulk.WriteToServer(dReader);
            excelConnection.Close();
        }
    }
}